//Processing Controller
hfrApp.registerCtrl("AddNewWordpressController", function ($scope, $http, $log, $window, $translate, $rootScope, $location, $timeout, HfrConfig, HfrStorage, HfrInit) {	
	
	
	
	//Init message alert
	HfrInit.message();
	
	//Delay content for display after 0,8 second
	$timeout(function () {
		$scope.showViewContent = true;
	}, 800);
	
	//Display meta header
	HfrConfig.displayMeta("addWordpress");
	
	//Remove blogId when add-new-site
	if ($location.path() == "/add-new-site-wordpress") {
		delete $rootScope.blogId;
		delete $rootScope.siteurl;
		HfrStorage.remove("blogId");
		$scope.username = "";
	}
	
	if ($rootScope.blogId === undefined) {
		if (HfrStorage.get("blogId") !== null) {
			$rootScope.blogId = HfrStorage.get("blogId");
		}
	}
	
	
	//Get content init with blogId
	$scope.editForm = false;
	$scope.pageTitle = $translate.instant("static.dashboard.createWordpress");
	if ($rootScope.blogId !== undefined) {
		$scope.editForm = true;
		$scope.pageTitle = $translate.instant("static.dashboard.editWordpress");
		
		var blogUrl = HfrConfig.ApiServiceUrl.blogsUrlApi + "/" + encodeURIComponent($rootScope.blogId);
		
		$scope.siteurl = $rootScope.blogId;
		
		$http.get(blogUrl)
			.success(function (data) {
				if (data.status == 200) {
					$scope.username = data.response.blog.username;
					$scope.password = data.response.blog.password;
				} else {
					$rootScope.reload.display = true;
					$rootScope.reload.text = data.message;
				}
			})
			.error(function (data) {
				$rootScope.reload.display = true;
				$rootScope.reload.text = $translate.instant("message.error.connect");
			});
		
	}
	
	$scope.check_info = function (form) {
		// Trigger validation flag && check valid form
	    $scope.submitted = true;
	    $scope.processing = true;
	    
		if (form.$invalid) {
//			console.log(form);
			$scope.processing = false;
	        return;
	    }
		
		if ($scope.editForm) {
			$scope.siteurl = $rootScope.blogId;
		}
		
		var checkUrl = HfrConfig.ApiServiceUrl.checkPostUrlApi + "?username=" + $scope.username
				+ "&password="+$scope.password
				+ "&type=Wordpress"
				+"&url=" + encodeURIComponent($scope.siteurl);
		
		$http.get(checkUrl).success(function (data) {
			if (data.status == 200) {
//				$scope.success = true;
//				$scope.processing = false;
//				$rootScope.message.success = true;
//				$rootScope.message.text = $translate.instant("message.addNewWordpress.checkPostSuccess");
//				$scope.next = true;
				
				//Submit and goto other page
				$scope.create_site(form);
				
			} else {
				$scope.success = false;
				$scope.processing = false;
				$rootScope.message.error = true;
				$rootScope.message.text = $translate.instant("message.addNewWordpress.checkPostError");
				$scope.next = false;
			}
			
		})
		.error(function(data) {
			$scope.processing = false;
	    	$scope.error = true;
	    	$rootScope.message.error = true;
	    	$rootScope.message.text = $translate.instant("message.error.connect");
	    });
		
		return false;
	};
	
	
	$scope.create_site = function (form) {
		$scope.processing = true;
		
		if ($scope.editForm) {
			$scope.siteurl = $rootScope.blogId;
		}
		
		//Proccsing data
		$rootScope.siteurl = $scope.siteurl;
		
		$scope.data = {
			blog:{
				url: $scope.siteurl,
				customerId: $rootScope.username,
				type: "Wordpress",
				username: $scope.username,
				password: $scope.password,
			}	
		};
		
		$http.post(HfrConfig.ApiServiceUrl.blogsUrlApi, $scope.data).success(function (data) {
			if (data.status == 200) {
				HfrStorage.set("siteUrl", $scope.siteurl);
				if ($scope.editForm) {
					$window.location.href= "#/site-management";
				} else {
					$window.location.href= "#/add-topics";
				}
			} else {
				$scope.error = true;
				$rootScope.message.error = true;
		    	$rootScope.message.text = data.message;
			}
			
			$scope.processing = false;
		})
	    .error(function(data) {
	    	$scope.error = true;
	    	$scope.processing = false;
	    	$rootScope.message.error = true;
	    	$rootScope.message.text = $translate.instant("message.error.connect");
	    	
	    });
		
		return false;
	}
});